package com.hsbc.dev.teamo4.sms.common.utils.data;

import com.google.common.collect.Lists;

import java.lang.reflect.*;
import java.util.Date;
import java.util.List;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.common.utils.data
 * @Description: ReflectionUtils
 * @Date date: 2018/7/29
 */
public class ReflectionUtils {

    /**
     * getFields
     * @param object
     * @return
     */
    public static Object[] getFields(Object object,List objectList) {
        Field[] fields = object.getClass().getDeclaredFields();
        List<Object> list= Lists.newArrayList();
        for (Field field : fields) {
            if(objectList!=null){
                if (objectList.contains(field.getName())) {
                    continue;
                }
            }
           Object result=getFieldValue(field,object);
           list.add(result);
        }
        Class<?> superClass = object.getClass().getSuperclass();
        if (null != superClass) {
            Field[] fields1=superClass.getDeclaredFields();
            for (Field field : fields1) {
                if(objectList!=null){
                    if (objectList.contains(field.getName())) {
                        continue;
                    }
                }
                Object result=getFieldValue(field,object);
                list.add(result);
            }
        }
        return list.toArray();
    }

    private static Object getFieldValue(Field field,Object object){
        Object value = null;
        // 如果类型是String
        try {
            if ("class java.lang.String".equals(field.getGenericType().toString())) {
                Method m = object.getClass().getMethod("get" + getMethodName(field.getName()));
                value = m.invoke(object);
            }else if("class java.lang.Integer".equals(field.getGenericType().toString())){
                Method m =  object.getClass().getMethod("get" + getMethodName(field.getName()));
                value = m.invoke(object);
            }else if ("class java.lang.Double".equals(field.getGenericType().toString())) {
                Method m = object.getClass().getMethod("get" + getMethodName(field.getName()));
                value = m.invoke(object);
            }else if ("class java.lang.Boolean".equals(field.getGenericType().toString())) {
                Method m =  object.getClass().getMethod("get"+upperCase(field.getName()));
                value =  m.invoke(object);
            }else if ("boolean".equals(field.getGenericType().toString())) {
                Method m =object.getClass().getMethod("get"+upperCase(field.getName()));
                value =m.invoke(object);
            }else if ("class java.util.Date".equals(field.getGenericType().toString())) {
                Method m = object.getClass().getMethod("get" + getMethodName(field.getName()));
                value = m.invoke(object);
            }else if ("class java.lang.Short".equals(field.getGenericType().toString())) {
                Method m =  object.getClass().getMethod("get" + getMethodName(field.getName()));
                value = m.invoke(object);
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    private static String getMethodName(String fildeName) throws Exception{
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    /**
     *  upperCase
     * @param str
     * @return
     */
    private static String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }


        /**
         * getNewInstance
         * @param clazz
         * @return
         * @throws InstantiationException
         * @throws IllegalAccessException
         */
    public static Object getNewInstance(Class<?> clazz) throws InstantiationException, IllegalAccessException {
        return clazz.newInstance();
    }



}
